/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.blocks;

import java.util.ArrayList;
import java.util.Iterator;

import fi.honkalampisaatio.lmodeller.blocks.Block.COLOR;
import fi.honkalampisaatio.lmodeller.blocks.Block.TYPE;
import fi.honkalampisaatio.lmodeller.xml.BlockData;

public class BlockBox {

	static BlockBox blockBox;
	ArrayList<Block> allBlocks = new ArrayList<Block>();
	
	public static BlockBox getBlockBox() {
		if(blockBox==null)
		{
			blockBox = new BlockBox();
		}
		return blockBox;
	}

	public void loadBlocks(ArrayList<BlockData> blocks)
	{
		this.allBlocks.clear();
		if(blocks==null)
		{
			return;
		}
		Iterator<BlockData> blockIterator = blocks.iterator();
		while(blockIterator.hasNext())
		{
			BlockData blockData = blockIterator.next();
			for(int i=0; i<blockData.getCount();i++)
			{
				Block piece = getBlock(blockData.getType(), blockData.getColor());
				if (piece!=null)
				{
				    this.allBlocks.add(piece);
				}
			}
		}
	}

	private Block getBlock(String type, String color) {
		Block block = new Block();
		try {
			block.setType(TYPE.valueOf(type.trim()));
			block.setColor(COLOR.valueOf(color.trim()));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return block;
	}

	@SuppressWarnings("unchecked")
	public ArrayList<Block> getCopyofAllBlocks() {
		return (ArrayList<Block>) this.allBlocks.clone();
	}
}
